package com.zhangsr.zus.remoting.api.transport;

import com.zhangsr.zus.common.URL;
import com.zhangsr.zus.common.serialize.Serialization;
import com.zhangsr.zus.common.serialize.support.hessian.Hessian2Serialization;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zhangsr
 */
public class CodecSupport {

    private static Logger logger = Logger.getLogger(CodecSupport.class);

    private static Map<Byte, Serialization> ID_SERIALIZATION_MAP = new HashMap<Byte, Serialization>();

    static {
        Serialization serialization = new Hessian2Serialization();
        byte idByte = serialization.getContentTypeId();
        if (ID_SERIALIZATION_MAP.containsKey(idByte)) {
            logger.error("Serialization extension " + serialization.getClass().getName()
                    + " has duplicate id to Serialization extension "
                    + ID_SERIALIZATION_MAP.get(idByte).getClass().getName()
                    + ", ignore this Serialization extension");
        }
        ID_SERIALIZATION_MAP.put(idByte, serialization);
    }

    public static Serialization getSerializationById(Byte id) {
        return ID_SERIALIZATION_MAP.get(id);
    }

    public static Serialization getSerialization(URL url) {
        Serialization serialization = new Hessian2Serialization();
        return serialization;
    }

    public static Serialization getSerialization(URL url, Byte id) {
        Serialization result = getSerializationById(id);
        if (result == null) {
            result = getSerialization(url);
        }
        return result;
    }

}
